package com.tanhua.api.mongo;

import com.tanhua.domain.mongo.Sound;
import org.apache.dubbo.config.annotation.Service;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @author ：DaLin
 * @date ：2021/1/12 10:24
 */
@Service
public class SoundApiImpl implements SoundApi {
    @Autowired
    private MongoTemplate mongoTemplate;
    @Override
    public void save(Sound sound) {
      //保存音频对象
        sound.setCreated(System.currentTimeMillis());
        mongoTemplate.save(sound);
    }

    @Override
    public List<Sound> find(Long userId) {
        Query query = Query.query(Criteria.where("state").is(0).and("userId").ne(userId));
        List<Sound> soundList = mongoTemplate.find(query, Sound.class);
        return soundList;
    }

    @Override
    public void update(String soundId) {
        Query query = Query.query(Criteria.where("id").is(new ObjectId(soundId)));
        Update update = new Update();
        update.set("state", 1);
        mongoTemplate.updateFirst(query, update, Sound.class);
    }
}
